﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using System.Data;
using System.Windows.Forms;

namespace tpsmi1002
{
    class CatalogueStock
    {
        private static CatalogueStock instance;
        private Dictionary<int, StockMarket> listStockMarkets;
        private Dictionary<int, Stock> listStocks;
        private Database db;

        private CatalogueStock() 
        {
            listStockMarkets = new Dictionary<int, StockMarket>();
            listStocks = new Dictionary<int, Stock>();
            
            db = Database.getInstance();

            loadStockData();
        }

        public static CatalogueStock getInstance()
        {
            if (instance == null)
                instance = new CatalogueStock();

            return instance;
        }

        // charger le contenu de la db dans les objets liste
        private void loadStockData()
        {
            string queryStock = "SELECT IDSTOCK, IDBOURSE, NOM, SYMBOLE FROM STOCK ORDER BY NOM";
            string queryStockMarket = "SELECT IDBOURSE, SYMBOLE, NOM FROM STOCK_EXCHANGES";

            DataSet dsSM = db.ExecuteDataSet(queryStockMarket);

            foreach (DataRow row in dsSM.Tables[0].Rows)
            {
                listStockMarkets.Add((int)row[0], new StockMarket((int)row[0], (string)row[1], (string)row[2]));
            }

            DataSet dsS = db.ExecuteDataSet(queryStock);

            foreach (DataRow row in dsS.Tables[0].Rows)
            {                
                listStocks.Add((int)row[0], new Stock((int)row[0], (string)row[2], (string)row[3], listStockMarkets[(int)row[1]]));
            }
        }

        // return la liste de stock market
        public Dictionary<int, StockMarket> getListStockMarkets()
        {
            return this.listStockMarkets;
        }

        // return la liste de stocks
        public Dictionary<int, Stock> getListStock()
        {
            return this.listStocks;
        }
    }
}
